package com.imageim.yimirror.ui.widght;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseApplication;
import com.imageim.yimirror.common.util.UIUtils;

/**
 * Created by Yuaihen.
 * on 2018/12/28
 */
public class BannerAnimView extends View {
    //内圈半径
    private int mRadius = UIUtils.dip2px(210);
    private static final int START_COLOR = BaseApplication.getContext().getResources().getColor(R.color.start_color, null);
    private static final int END_COLOR = BaseApplication.getContext().getResources().getColor(R.color.end_color, null);
    private static final int RING_START_COLOR = BaseApplication.getContext().getResources().getColor(R.color.ring_start_color, null);
    private static final int RING_END_COLOR = BaseApplication.getContext().getResources().getColor(R.color.ring_end_color, null);
    private String text = "触摸开始";
    private Paint mTextPaint;
    private int mInsideRadius;
    private int mInsideWidth;
    private int mReadWidth;
    private int mX;
    private int mY;
    //圆环半径
    private int mOuterRadius = UIUtils.dip2px(29);
    private int mTextSize = UIUtils.sp2px(65);
    private Paint mRingPaint;
    private Paint mCirclePaint;
    //圆环的初始半径和最大半径
    private float mRingMaxRadius;
    private float mRingMinRadius;
    //圆环的初始宽度和最大宽度
    private static final int RING_WIDTH = UIUtils.dip2px(40);
    private float mRingMinWidth = 0;
    private int mRingMaxWidth = RING_WIDTH;
    private LinearGradient mRingGradient;
    private LinearGradient mCircleGradient;

    public BannerAnimView(Context context) {
        super(context);
        init(context);
    }

    public BannerAnimView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BannerAnimView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        //内圈
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setDither(true);
        mCirclePaint.setStyle(Paint.Style.FILL);
        mCirclePaint.setStrokeCap(Paint.Cap.ROUND);

        mTextPaint = new Paint();
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setDither(true);
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));

        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setDither(true);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeCap(Paint.Cap.ROUND);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //默认四个padding一致
        int expect = mRadius * 2 + mRingMaxWidth + getPaddingLeft() + getPaddingRight();

        //View的宽高(包含圆形和画笔的宽度)
        int width = resolveSize(expect, widthMeasureSpec);
        int height = resolveSize(expect, widthMeasureSpec);
        //源码方法判断Mode 对应的Size
        mReadWidth = Math.min(width, height);
        //内圈宽度
        mInsideWidth = mReadWidth - mRingMaxWidth * 2;
        //内圈半径
        mInsideRadius = mInsideWidth / 2;

        mX = mReadWidth / 2;
        mY = mReadWidth / 2;

        //圆环初始半径和内圈一一致  最大半径为mReadWidth / 2
        mRingMinRadius = mInsideRadius - mRingMaxWidth / 2f;
        mRingMaxRadius = mReadWidth / 2 - mRingMaxWidth / 2;
        setMeasuredDimension(mReadWidth, mReadWidth);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        //        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        //        canvas.translate(getPaddingLeft() + mOuterRadius, getPaddingTop() + mOuterRadius);

        //        canvas.save();
        //        canvas.translate(mRingMaxWidth / 2, mRingMaxWidth / 2);
        //绘制圆环  由于圆环本身有宽度，所以半径要减去圆环宽度的一半，不然一部分圆会在view外面
        mRingPaint.setStrokeWidth(mRingMinWidth);
        if (mRingGradient == null) {
            mRingGradient = new LinearGradient(
                    0, 0, mReadWidth, 0,
                    RING_START_COLOR, RING_END_COLOR, Shader.TileMode.CLAMP);
        }
        mRingPaint.setShader(mRingGradient);
        canvas.drawCircle(mX, mY, mRingMinRadius, mRingPaint);
        //        canvas.restore();


        //绘制内圈
        if (mCircleGradient == null) {
            mCircleGradient = new LinearGradient(
                    mRingMaxWidth, mRingMaxWidth, mReadWidth - mRingMaxWidth, mRingMaxWidth,
                    START_COLOR, END_COLOR, Shader.TileMode.CLAMP);
        }
        mCirclePaint.setShader(mCircleGradient);
        canvas.drawCircle(mX, mY, mInsideRadius, mCirclePaint);


        //绘制文字
//        canvas.drawText(text, mInsideRadius - (UIUtils.getTextWidth(text, mTextPaint) / 2.5f), mReadWidth / 2 + (UIUtils.getTextHeight(text, mTextPaint) / 2.5f), mTextPaint);

        if (mRingMinWidth >= mRingMaxWidth && mRingMinRadius >= mRingMaxRadius) {
            mRingMinRadius = mInsideRadius - mRingMaxWidth / 2f;
            mRingMinWidth = 0;
            invalidate();
        } else {
            mRingMinWidth += 1;
            mRingMinRadius += 1;
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    invalidate();
                }
            }, 20);
            //            try {
            //                Thread.sleep(25);
            //                invalidate();
            //            } catch (InterruptedException e) {
            //                e.printStackTrace();
            //            }
        }
    }


}
